package com.devbobcorn.donutopia.entities;

import com.devbobcorn.donutopia.Donutopia;
import com.devbobcorn.donutopia.client.event.rendering.ChoxRenderer;
import com.devbobcorn.donutopia.client.event.rendering.GingerBreadManRenderer;
import com.devbobcorn.donutopia.items.ModItemTab;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.EntitySpawnPlacementRegistry;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.item.Item;
import net.minecraft.world.gen.Heightmap;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.common.ForgeSpawnEggItem;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;

@EventBusSubscriber(modid = Donutopia.MODID, bus = EventBusSubscriber.Bus.MOD)
public class ModEntityTypes {
    private static final Logger LOGGER = LogManager.getLogger(Donutopia.MODID + " Entity Types");

    public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, Donutopia.MODID);

    public static final EntityType<ChoxEntity> CHOX = EntityType.Builder.<ChoxEntity>of(ChoxEntity::new, EntityClassification.CREATURE).sized(0.6F, 0.7F).setShouldReceiveVelocityUpdates(true).setTrackingRange(8).setUpdateInterval(3).setCustomClientFactory(ChoxEntity::new).build("chox");
    public static final RegistryObject<EntityType<ChoxEntity>> $CHOX = ENTITY_TYPES.register("chox", () -> CHOX);
    public static final EntityType<GingerBreadManEntity> GINGER_BREAD_MAN = EntityType.Builder.<GingerBreadManEntity>of(GingerBreadManEntity::new, EntityClassification.MONSTER).sized(0.6F, 1.85F).setShouldReceiveVelocityUpdates(true).setTrackingRange(12).setUpdateInterval(2).setCustomClientFactory(GingerBreadManEntity::new).build("ginger_bread_man");
    public static final RegistryObject<EntityType<GingerBreadManEntity>> $GINGER_BREAD_MAN = ENTITY_TYPES.register("ginger_bread_man", () -> GINGER_BREAD_MAN);

    @SubscribeEvent
    public static void registerPlacements(FMLCommonSetupEvent event) {
        EntitySpawnPlacementRegistry.register(CHOX, EntitySpawnPlacementRegistry.PlacementType.ON_GROUND, Heightmap.Type.MOTION_BLOCKING_NO_LEAVES,
            (entityType, world, reason, pos, random) -> (world.getBlockState(pos.below()).getMaterial() == Material.GRASS && world.getMaxLocalRawBrightness(pos, 0) > 8));
        EntitySpawnPlacementRegistry.register(GINGER_BREAD_MAN, EntitySpawnPlacementRegistry.PlacementType.ON_GROUND, Heightmap.Type.MOTION_BLOCKING_NO_LEAVES,
            (entityType, world, reason, pos, random) -> (world.getBlockState(pos.below()).getMaterial() == Material.GRASS && world.getMaxLocalRawBrightness(pos, 0) < 14));
        
        LOGGER.info("Entity Placement Registered.");
    }

    @SubscribeEvent
    public static void registerAttributes(EntityAttributeCreationEvent event) {
        event.put(ModEntityTypes.CHOX, MobEntity.createMobAttributes().add(Attributes.MOVEMENT_SPEED, 0.3).add(Attributes.MAX_HEALTH, 10).add(Attributes.ARMOR, 0).add(Attributes.ATTACK_DAMAGE, 3).build());
        event.put(ModEntityTypes.GINGER_BREAD_MAN, MobEntity.createMobAttributes().add(Attributes.MOVEMENT_SPEED, 0.3).add(Attributes.MAX_HEALTH, 15).add(Attributes.ARMOR, 0).add(Attributes.ATTACK_DAMAGE, 2).build());

        LOGGER.info("Entity Attributes Registered.");
    }

    @SubscribeEvent
    @OnlyIn(Dist.CLIENT)
    public static void registerModels(ModelRegistryEvent event) {
        RenderingRegistry.registerEntityRenderingHandler(ModEntityTypes.CHOX, renderManager -> new ChoxRenderer(renderManager));
        RenderingRegistry.registerEntityRenderingHandler(ModEntityTypes.GINGER_BREAD_MAN, renderManager -> new GingerBreadManRenderer(renderManager));

        LOGGER.info("Entity Renderers Registered.");
    }

    public static final Item CHOX_SPAWN_EGG = new ForgeSpawnEggItem($CHOX, -6533355, -10802430, new Item.Properties().tab(ModItemTab.DONUTOPIA_GROUP)).setRegistryName("chox_spawn_egg");
    public static final Item GINGER_BREAD_MAN_SPAWN_EGG = new ForgeSpawnEggItem($GINGER_BREAD_MAN, -4364524, -1, new Item.Properties().tab(ModItemTab.DONUTOPIA_GROUP)).setRegistryName("ginger_bread_man_spawn_egg");

    @SubscribeEvent
    public static void registerSpawnEggs(final RegistryEvent.Register<Item> event){
        IForgeRegistry<Item> registry = event.getRegistry();

        registry.register(CHOX_SPAWN_EGG);
        registry.register(GINGER_BREAD_MAN_SPAWN_EGG);
    }
}
